<template>
  <view class="example-container">
    <!-- 案例列表 -->
    <view class="example-list">
      <view class="example-item" v-for="(item, index) in demoList" :key="index">
        <!-- 轮播图 -->
        <swiper class="example-swiper" 
          :indicator-dots="true" 
          indicator-color="rgba(255, 255, 255, 0.5)"
          indicator-active-color="#ffffff"
          :autoplay="true" 
          :interval="3000" 
          :duration="1000"
          circular
        >
          <swiper-item v-for="(img, imgIndex) in item.images" :key="imgIndex">
            <image class="example-image" :src="img" mode="aspectFill"></image>
          </swiper-item>
        </swiper>
        
        <view class="example-info">
          <text class="example-title">{{item.title}}</text>
          <text class="example-desc">{{item.description}}</text>
          <view class="example-data">
            <text class="data-item">{{item.sessions}}次</text>
            <text class="data-item">脱毛效果{{item.effect}}%</text>
          </view>
        </view>
      </view>
    </view>

    <!-- 加载更多提示 -->
    <view class="loading-more" v-if="loading">加载中...</view>
    <!-- 无数据提示 -->
    <view class="empty-tip" v-if="demoList.length === 0">
      <image src="/static/images/index_page.png" mode="aspectFit" class="empty-icon"/>
      <text>暂无案例</text>
    </view>
  </view>
</template>

<script>
import config from '@/config/config.js'

export default {
  data() {
    return {
      demoList: [],
      pageNum: 1,
      pageSize: 10,
      hasMore: true,
      loading: false
    }
  },
  methods: {
    // 获取案例列表
    async getDemoList(refresh = false) {
      if (this.loading) return
      this.loading = true
      
      try {
        if (refresh) {
          this.pageNum = 1
          this.hasMore = true
          this.demoList = []
        }

        const result = await this.$http.post(config.api.slconfig.slDemoList, {
          pageNum: this.pageNum,
          pageSize: this.pageSize
        })
        
        console.log('案例列表响应:', result)
        
        if (result.data.code === 200 || result.data.code === 201) {
          const demos = result.data.rows || []
          
          // 判断是否还有更多数据
          if (demos.length < this.pageSize) {
            this.hasMore = false
          }
          
          // 处理数据并添加到列表
          const formattedDemos = demos.map(item => ({
            id: item.id,
            title: item.demoTitle,
            description: item.demoText,
            images: item.swiperImgs ? (
              typeof item.swiperImgs === 'string' 
                ? item.swiperImgs.split(',') 
                : Array.isArray(item.swiperImgs) 
                  ? item.swiperImgs 
                  : ['../../static/images/index_page.png']
            ) : ['../../static/images/index_page.png'],
            sessions: item.demoTimes || 3,
            effect: item.demoRadios || 80
          }))
          
          this.demoList = refresh ? formattedDemos : [...this.demoList, ...formattedDemos]
        }
      } catch (err) {
        console.error('获取案例列表失败:', err)
        uni.showToast({
          title: err.message || '获取案例列表失败',
          icon: 'none'
        })
      } finally {
        this.loading = false
        uni.hideLoading()
      }
    }
  },
  
  async onLoad() {
    try {
      uni.showLoading({
        title: '加载中...'
      })
      await this.getDemoList(true)
    } catch (err) {
      console.error('页面加载失败:', err)
      uni.showToast({
        title: '加载失败',
        icon: 'none'
      })
    } finally {
      uni.hideLoading()
    }
  },
  
  // 下拉刷新
  async onPullDownRefresh() {
    try {
      await this.getDemoList(true)
    } catch (err) {
      console.error('刷新失败:', err)
    } finally {
      uni.stopPullDownRefresh()
    }
  },
  
  // 上拉加载更多
  async onReachBottom() {
    if (!this.hasMore || this.loading) return
    
    this.pageNum++
    await this.getDemoList()
  }
}
</script>

<style>
.example-container {
  padding: 20rpx;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.example-list {
  display: flex;
  flex-direction: column;
  gap: 20rpx;
}

.example-item {
  background-color: #fff;
  border-radius: 12rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
}

.example-swiper {
  width: 100%;
  height: 400rpx;
}

.example-image {
  width: 100%;
  height: 100%;
  background-color: #f5f5f5;
}

/* 修改轮播图指示点的位置 */
:deep(.uni-swiper-dots) {
  bottom: 20rpx;
}

:deep(.uni-swiper-dot) {
  width: 12rpx;
  height: 12rpx;
  border-radius: 6rpx;
  margin: 0 6rpx;
}

.example-info {
  padding: 20rpx;
}

.example-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 10rpx;
  display: block;
}

.example-desc {
  font-size: 28rpx;
  color: #666;
  line-height: 1.6;
  margin-bottom: 20rpx;
  display: block;
}

.example-data {
  display: flex;
  gap: 30rpx;
}

.data-item {
  font-size: 26rpx;
  color: #999;
  background-color: #f8f8f8;
  padding: 6rpx 20rpx;
  border-radius: 20rpx;
}

.loading-more {
  text-align: center;
  padding: 20rpx 0;
  color: #999;
  font-size: 24rpx;
}

.empty-tip {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding-top: 100rpx;
  color: #999;
}

.empty-icon {
  width: 160rpx;
  height: 160rpx;
  margin-bottom: 30rpx;
  opacity: 0.5;
}
</style> 